using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using logistical.Models;

namespace logistical.Controllers
{
    public class base_companyinfoController : baseController
    {
        public class list_base_companyinfo
        {
            public List<base_companyinfo> objs { get; set; }
        }

        [HttpGet]
        public object get()
        {
            try
            {
                var o = new logisticalEntities();
                var p = o.base_companyinfo;

                return new { 
                                data = p

                            };
            }
            catch (Exception ex)
            {
                return new { msg = ex.Message };
            }
        }

[HttpGet]
                                        public object get(string companyid)
                                        {
                                            try
                                            {
                                                var o = new logisticalEntities();
                                                var p = o.base_companyinfo.Where(x => x.companyid == companyid);
                                                
                                                return new
                                                {
                                                    data = p
                                                
                                                };
                                            }
                                            catch (Exception ex)
                                            {
                                                return new { msg = ex.Message };
                                            }
                                        }

        [HttpPost]
        public object add(base_companyinfo obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                obj.id = Guid.NewGuid().ToString();
                common.sysEval.evalCurUserAndDate(obj);
                o.base_companyinfo.Add(obj);
                o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object edit(base_companyinfo obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.base_companyinfo.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.base_companyinfo.SingleOrDefault(x => x.id == obj.id);
old.contact=obj.contact;
old.mobile=obj.mobile;
old.address=obj.address;
old.remark=obj.remark;
old.companyid=obj.companyid;
common.sysEval.evalCurUserAndDate(old);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object del(base_companyinfo obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.base_companyinfo.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.base_companyinfo.SingleOrDefault(x => x.id == obj.id);
                    o.base_companyinfo.Remove(old);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object mdel(list_base_companyinfo objs)
        {
            string msg = "";
            try
            {
            var o = new logisticalEntities();
            foreach (var p in objs.objs)
            {
                var co = o.base_companyinfo.Where(x => x.id == p.id);
                if (co.Count() == 1)
                {
                    var old = o.base_companyinfo.SingleOrDefault(x => x.id == p.id);
                    o.base_companyinfo.Remove(old);
                }
            }
            o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }
    }
}
